Package org.javacommerce.paypal.ws

Source Code of org.javacommerce.paypal.ws.API

/**
*
*/
package org.javacommerce.paypal.ws;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.avalon.framework.service.ServiceException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.javacommerce.core.config.JavaCommerce;
import org.javacommerce.core.log.LogMessage;
import org.javacommerce.core.log.LogMessageFactory;

import CoreComponentTypes.apis.ebay.BasicAmountType;
import PayPalAPI.api.ebay.AddressVerifyReq;
import PayPalAPI.api.ebay.AddressVerifyResponseType;
import PayPalAPI.api.ebay.BAUpdateRequestType;
import PayPalAPI.api.ebay.BAUpdateResponseType;
import PayPalAPI.api.ebay.BillAgreementUpdateReq;
import PayPalAPI.api.ebay.BillUserReq;
import PayPalAPI.api.ebay.BillUserRequestType;
import PayPalAPI.api.ebay.BillUserResponseType;
import PayPalAPI.api.ebay.DoAuthorizationReq;
import PayPalAPI.api.ebay.DoAuthorizationRequestType;
import PayPalAPI.api.ebay.DoAuthorizationResponseType;
import PayPalAPI.api.ebay.DoCaptureReq;
import PayPalAPI.api.ebay.DoCaptureRequestType;
import PayPalAPI.api.ebay.DoCaptureResponseType;
import PayPalAPI.api.ebay.DoDirectPaymentReq;
import PayPalAPI.api.ebay.DoDirectPaymentRequestType;
import PayPalAPI.api.ebay.DoDirectPaymentResponseType;
import PayPalAPI.api.ebay.DoExpressCheckoutPaymentReq;
import PayPalAPI.api.ebay.DoExpressCheckoutPaymentRequestType;
import PayPalAPI.api.ebay.DoExpressCheckoutPaymentResponseType;
import PayPalAPI.api.ebay.DoReauthorizationReq;
import PayPalAPI.api.ebay.DoReauthorizationRequestType;
import PayPalAPI.api.ebay.DoReauthorizationResponseType;
import PayPalAPI.api.ebay.DoVoidReq;
import PayPalAPI.api.ebay.DoVoidRequestType;
import PayPalAPI.api.ebay.DoVoidResponseType;
import PayPalAPI.api.ebay.EnterBoardingReq;
import PayPalAPI.api.ebay.EnterBoardingResponseType;
import PayPalAPI.api.ebay.GetBoardingDetailsReq;
import PayPalAPI.api.ebay.GetBoardingDetailsResponseType;
import PayPalAPI.api.ebay.GetExpressCheckoutDetailsReq;
import PayPalAPI.api.ebay.GetExpressCheckoutDetailsRequestType;
import PayPalAPI.api.ebay.GetExpressCheckoutDetailsResponseType;
import PayPalAPI.api.ebay.GetTransactionDetailsReq;
import PayPalAPI.api.ebay.GetTransactionDetailsRequestType;
import PayPalAPI.api.ebay.GetTransactionDetailsResponseType;
import PayPalAPI.api.ebay.MassPayReq;
import PayPalAPI.api.ebay.MassPayRequestType;
import PayPalAPI.api.ebay.MassPayResponseType;
import PayPalAPI.api.ebay.PayPalAPIAAInterface;
import PayPalAPI.api.ebay.PayPalAPIInterface;
import PayPalAPI.api.ebay.RefundTransactionReq;
import PayPalAPI.api.ebay.RefundTransactionRequestType;
import PayPalAPI.api.ebay.RefundTransactionResponseType;
import PayPalAPI.api.ebay.SetExpressCheckoutReq;
import PayPalAPI.api.ebay.SetExpressCheckoutRequestType;
import PayPalAPI.api.ebay.SetExpressCheckoutResponseType;
import PayPalAPI.api.ebay.TransactionSearchReq;
import PayPalAPI.api.ebay.TransactionSearchRequestType;
import PayPalAPI.api.ebay.TransactionSearchResponseType;
import eBLBaseComponents.apis.ebay.AbstractRequestType;
import eBLBaseComponents.apis.ebay.AbstractResponseType;
import eBLBaseComponents.apis.ebay.AckCodeType;
import eBLBaseComponents.apis.ebay.CurrencyCodeType;
import eBLBaseComponents.apis.ebay.DoDirectPaymentRequestDetailsType;
import eBLBaseComponents.apis.ebay.DoExpressCheckoutPaymentRequestDetailsType;
import eBLBaseComponents.apis.ebay.ErrorType;
import eBLBaseComponents.apis.ebay.MerchantPullPaymentType;
import eBLBaseComponents.apis.ebay.PaymentDetailsType;
import eBLBaseComponents.apis.ebay.PaymentTransactionClassCodeType;
import eBLBaseComponents.apis.ebay.PaymentTransactionStatusCodeType;
import eBLBaseComponents.apis.ebay.PersonNameType;
import eBLBaseComponents.apis.ebay.RefundPurposeTypeCodeType;
import eBLBaseComponents.apis.ebay.SetExpressCheckoutRequestDetailsType;
import eBLBaseComponents.apis.ebay.SeverityCodeType;

/**
* The API class wraps PayPal's Web Services interfaces.  Configuration is read from javacommerce.properties, located
* in the CLASSPATH.  Current values read are:
<table class="bodyTable">
<tr class="a">
<th>Field Name</th>
<th>Description</th>
<th>Required</th>
</tr>
<tr class="b">
<td>paypal.api.username</td>
<td>API Username provided by PayPal</td>
<td>Yes</td>
</tr>
<tr class="a">
<td>paypal.api.password</td>
<td>API Password provided by PayPal</td>
<td>Yes</td>
</tr>
<tr class="b">
<td>paypal.api.signature</td>
<td>API Signature provided by PayPal</td>
<td>Yes if you're using Signature Authentication</td>
</tr>
<tr class="a">
<td>paypal.api.timeout</td>
<td>API timeout to use for API calls (in milliseconds)</td>
<td>No, default is 30000 (30s)</td>
</tr>
<tr class="b">
<td>paypal.api.url</td>
<td>URL to use when communicating with Standard API calls using Client Certificates</td>
<td>No, see <a href="#URLs">PayPal URLs</a></td>
</tr>
<tr class="a">
<td>paypal.api.3t.url</td>
<td>URL to use when communicating with Standard API calls using Signature Authentication
</td>
<td>No, see <a href="#URLs">PayPal URLs</a></td>
</tr>
<tr class="b">
<td>paypal.api.retry</td>
<td>Number of times to retry the API call with PayPal.  <em>NOTE:</em> This will only
retry the API call if the request was not sent to PayPal.</td>
<td>No, defaults to 3</a></td>
</tr>
</table>
* @author Michael Blanton (mike@mikeblanton.com)
* @link http://javacommerce.sourceforge.net/paypal-ws-tools/config.html
*/
public class API {

  private static final String CONFIG_PAYPAL_API_SIGNATURE = "paypal.api.signature";
  private static final String CONFIG_PAYPAL_API_PASSWORD = "paypal.api.password";
  private static final String CONFIG_PAYPAL_API_USERNAME = "paypal.api.username";
  /**
   * Default API credentials as loaded from the javacommerce.properties file.
   * Currently read values are:
   * <ul>
   * <li>paypal.api.username
   * <li>paypal.api.password
   * <li>paypal.api.signature
   * </ul>
   */
  public static APICredential DEFAULT_CREDENTIALS;
  private static final String CONFIG_PAYPAL_API_URL = "paypal.api.url";
  private static final String CONFIG_PAYPAL_API_AA_URL = "paypal.api.aa.url";
  /**
   * @deprecated Use paypal.api.url instead.
   */
  private static final String CONFIG_PAYPAL_API_3T_URL = "paypal.api.3t.url";
  /**
   * @deprecated Use paypal.api.aa.url instead.
   */
  private static final String CONFIG_PAYPAL_API_3T_AA_URL = "paypal.api.3t.aa.url";
  private static final String PARAM_PAYPAL_API_URL = "javacommerce.paypal.api.url";
  private static final String PARAM_PAYPAL_API_AA_URL = "javacommerce.paypal.api.aa.url";
  /**
   * @deprecated Use javacommerce.paypal.api.url instead.
   */
  private static final String PARAM_PAYPAL_API_3T_URL = "javacommerce.paypal.api.3t.url";
  /**
   * @deprecated Use javacommerce.paypal.api.aa.url instead.
   */
  private static final String PARAM_PAYPAL_API_3T_AA_URL = "javacommerce.paypal.api.3t.aa.url";
  private static final String CONFIG_PAYPAL_API_TIMEOUT = "paypal.api.timeout";
  private static final String PARAM_PAYPAL_API_TIMEOUT = "javacommerce.paypal.api.timeout";
  private static final String CONFIG_PAYPAL_MAX_RETRY = "paypal.api.retry";
  private static final String PARAM_PAYPAL_MAX_RETRY = "javacommerce.paypal.api.retry";
  private static final String CURRENT_VERSION = "2.3";
 
  private transient static Map<String, PayPalAPIInterface> API_INTERFACE_MAP = new HashMap<String, PayPalAPIInterface>();
  private transient static Map<String, PayPalAPIAAInterface> API_AA_INTERFACE_MAP = new HashMap<String, PayPalAPIAAInterface>();
  private static final int INTERFACE_DEFAULT_TIMEOUT = 30000;
  private static final int MAX_RETRY_DEFAULT = 3;
  private static URL API_URL = null;
  private static URL API_AA_URL = null;
  private static URL API_3T_URL = null;
  private static URL API_3T_AA_URL = null;
  private static Integer INTERFACE_TIMEOUT = null;
  private static Integer MAX_RETRY = null;

  private static final Log LOG = LogFactory.getLog(API.class);
  private static final LogMessage LOGM = LogMessageFactory.getInstance();

  static {
    DEFAULT_CREDENTIALS = new APICredential(JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_USERNAME), JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_PASSWORD), JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_SIGNATURE));
  }
 
  public static final void init(String _username, String _password, String _signature) {
    DEFAULT_CREDENTIALS = new APICredential(_username, _password, _signature);
 

  public static final PayPalAPIInterface getAPIInterface() {
    return getAPIInterface(DEFAULT_CREDENTIALS);
  }
 
  public static final PayPalAPIInterface getAPIInterface(APICredential _credentials)  {
    String key = _credentials.getKey();
    if (!API_INTERFACE_MAP.containsKey(key)) {
      PayPalAPIInterface api = new NVPAPIImpl(_credentials);
      API_INTERFACE_MAP.put(key, api);
    }
    return (PayPalAPIInterface) API_INTERFACE_MAP.get(key);
  }
 
  public static final PayPalAPIAAInterface getAPIAAInterface() {
    return getAPIAAInterface(DEFAULT_CREDENTIALS);
  }
 
  public static final PayPalAPIAAInterface getAPIAAInterface(APICredential _credentials) {
    String key = _credentials.getKey();
    if (!API_AA_INTERFACE_MAP.containsKey(key)) {
      PayPalAPIAAInterface api = new NVPAAAPIImpl(_credentials);
      API_AA_INTERFACE_MAP.put(key, api);
    }
    return (PayPalAPIAAInterface) API_AA_INTERFACE_MAP.get(key);
  }
 
  /**
   * Retrieves the Endpoint URL based on the given criteria in the given order:
   * <UL>
   * <LI>Configuration File Parameter <code>api.url</code>.</LI>
   * <LI>System Property <code>paypaltools.api.url</code>.</LI>
   * </UL>
   * @return
   */
  protected static final URL getApiUrl() {
    if (API_URL == null) {
      try {
        if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_API_URL)) {
          API_URL = new URL(JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_URL));
        }
        else if (System.getProperty(PARAM_PAYPAL_API_URL) != null) {
          API_URL = new URL(System.getProperty(PARAM_PAYPAL_API_URL));
        }
      } catch (MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
          LOG.error("Error reading PayPal API URL: " + e.getLocalizedMessage(), e);
        }
      }
    }
    return API_URL;
  }
 
  /**
   * Retrieves the Endpoint URL based on the given criteria in the given order:
   * <UL>
   * <LI>Configuration File Parameter <code>api.url</code>.</LI>
   * <LI>System Property <code>paypaltools.api.url</code>.</LI>
   * </UL>
   * @return
   * @deprecated Use getApiUrl() instead.
   */
  protected static final URL getApi3tUrl() {
    if (API_3T_URL == null) {
      try {
        if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_API_3T_URL)) {
          API_3T_URL = new URL(JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_3T_URL));
        }
        else if (System.getProperty(PARAM_PAYPAL_API_3T_URL) != null) {
          API_3T_URL = new URL(System.getProperty(PARAM_PAYPAL_API_3T_URL));
        }
      } catch (MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
          LOG.error("Error reading PayPal API URL: " + e.getLocalizedMessage(), e);
        }
      }
    }
    return API_3T_URL;
  }
 
  /**
   * Retrieves the Endpoint AA URL based on the given criteria in the given order:
   * <UL>
   * <LI>Configuration File Parameter <code>api.aa.url</code>.</LI>
   * <LI>System Property <code>paypaltools.api.aa.url</code>.</LI>
   * </UL>
   * @return
   * @since 1.0b4(1.1)
   */
  protected static final URL getApiAAUrl() {
    if (API_AA_URL == null) {
      try {
        if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_API_AA_URL)) {
          API_AA_URL = new URL(JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_AA_URL));
        }
        else if (System.getProperty(PARAM_PAYPAL_API_AA_URL) != null) {
          API_AA_URL = new URL(System.getProperty(PARAM_PAYPAL_API_AA_URL));
        }
      } catch (MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
          LOG.error("Error reading PayPal API AA URL: " + e.getLocalizedMessage(), e);
        }
      }
    }
    return API_AA_URL;
  }
 
  /**
   * Retrieves the Endpoint 3T AA URL based on the given criteria in the given order:
   * <UL>
   * <LI>Configuration File Parameter <code>api.aa.url</code>.</LI>
   * <LI>System Property <code>paypaltools.api.aa.url</code>.</LI>
   * </UL>
   * @return
   * @deprecated Use getApiAAUrl() instead.
   */
  protected static final URL getApi3tAAUrl() {
    if (API_3T_AA_URL == null) {
      try {
        if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_API_3T_AA_URL)) {
          API_3T_AA_URL = new URL(JavaCommerce.getConfiguration().getString(CONFIG_PAYPAL_API_3T_AA_URL));
        }
        else if (System.getProperty(PARAM_PAYPAL_API_3T_AA_URL) != null) {
          API_3T_AA_URL = new URL(System.getProperty(PARAM_PAYPAL_API_3T_AA_URL));
        }
      } catch (MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
          LOG.error("Error reading PayPal API 3T AA URL: " + e.getLocalizedMessage(), e);
        }
      }
    }
    return API_3T_AA_URL;
  }
 
  /**
   * Retrieves the Timeout based on the given criteria in the given order:
   * <UL>
   * <LI>Configuration File Parameter <code>api.timeout</code>.</LI>
   * <LI>System Property <code>paypaltools.api.timeout</code>.</LI>
   * </UL>
   * @return
   */
  protected static final int getApiTimeout() {
    if (INTERFACE_TIMEOUT == null) {
      if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_API_TIMEOUT)) {
        INTERFACE_TIMEOUT = new Integer(JavaCommerce.getConfiguration().getInt(CONFIG_PAYPAL_API_TIMEOUT));
      }
      else {
        INTERFACE_TIMEOUT = new Integer(System.getProperty(PARAM_PAYPAL_API_TIMEOUT, String.valueOf(INTERFACE_DEFAULT_TIMEOUT)));
      }
    }
    return INTERFACE_TIMEOUT.intValue();
  }
 
  protected static final int getApiRetry() {
    if (MAX_RETRY == null) {
      if (JavaCommerce.getConfiguration() != null && JavaCommerce.getConfiguration().containsKey(CONFIG_PAYPAL_MAX_RETRY)) {
        MAX_RETRY = new Integer(JavaCommerce.getConfiguration().getInt(CONFIG_PAYPAL_MAX_RETRY));
      }
      else {
        MAX_RETRY = new Integer(System.getProperty(PARAM_PAYPAL_MAX_RETRY, String.valueOf(MAX_RETRY_DEFAULT)));
      }
    }
    return MAX_RETRY.intValue();
  }
 
  /**
   * Validates the passed AbstractRequestType, ensuring it has the required fields set.
   * <em>NOTE:</em>As of the August 2005 PayPal release, the Version should be "2.0".
   * @param _request
   */
  public static final void validateRequest(AbstractRequestType _request) {
    if (_request.getVersion() == null || _request.getVersion().length() == 0) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Version not set in request, setting to [" + CURRENT_VERSION + "]");
      }
      _request.setVersion(CURRENT_VERSION);
    }
   
    // Add a ButtonSource value.
    if (_request instanceof DoDirectPaymentRequestType) {
      DoDirectPaymentRequestType dpReq = (DoDirectPaymentRequestType) _request;
      if (dpReq.getDoDirectPaymentRequestDetails() == null) {
        dpReq.setDoDirectPaymentRequestDetails(new DoDirectPaymentRequestDetailsType());
      }
      if (dpReq.getDoDirectPaymentRequestDetails().getPaymentDetails() == null) {
        dpReq.getDoDirectPaymentRequestDetails().setPaymentDetails(new PaymentDetailsType());
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug("Setting ButtonSource to [" + JavaCommerce.VERSION + "]");
      }
      dpReq.getDoDirectPaymentRequestDetails().getPaymentDetails().setButtonSource(JavaCommerce.VERSION);
    }
    else if (_request instanceof DoExpressCheckoutPaymentRequestType) {
      DoExpressCheckoutPaymentRequestType ecReq = (DoExpressCheckoutPaymentRequestType) _request;
      if (ecReq.getDoExpressCheckoutPaymentRequestDetails() == null) {
        ecReq.setDoExpressCheckoutPaymentRequestDetails(new DoExpressCheckoutPaymentRequestDetailsType());
      }
      if (ecReq.getDoExpressCheckoutPaymentRequestDetails().getPaymentDetails() == null) {
        ecReq.getDoExpressCheckoutPaymentRequestDetails().setPaymentDetails(new PaymentDetailsType());
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug("Setting ButtonSource to [" + JavaCommerce.VERSION + "]");
      }
      ecReq.getDoExpressCheckoutPaymentRequestDetails().getPaymentDetails().setButtonSource(JavaCommerce.VERSION);
    }
    else if (_request instanceof BillUserRequestType) {
      BillUserRequestType buReq = (BillUserRequestType) _request;
      if (buReq.getMerchantPullPaymentDetails() == null) {
        buReq.setMerchantPullPaymentDetails(new MerchantPullPaymentType());
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug("Setting ButtonSource to [" + JavaCommerce.VERSION + "]");
      }
      buReq.getMerchantPullPaymentDetails().setButtonSource(JavaCommerce.VERSION);
    }
  }
 
  /**
   * Processes the AbstractResponseType instance, logging all errors and warnings as appropriate.
   * @param _response
   */
  public static final void processResponse(AbstractResponseType _response) {
    if (_response.getErrors() != null && _response.getErrors().length > 0) {
      for (int i = 0; i < _response.getErrors().length; i++) {
        ErrorType error = _response.getErrors(i);
        if (error.getSeverityCode().equals(SeverityCodeType.Warning) && LOG.isWarnEnabled()) {
          LOG.warn(new StringBuffer("Warning processing PayPal Request: ").append(error.getShortMessage()).append(" - ").append(error.getLongMessage()).append(" (").append(error.getErrorCode().toString()).append(")").toString());
        }
        else if (LOG.isErrorEnabled()) {
          LOG.error(new StringBuffer("Error processing PayPal Request: ").append(error.getShortMessage()).append(" - ").append(error.getLongMessage()).append(" (").append(error.getErrorCode().toString()).append(")").toString());
        }
      }
    }

  }
 
  /**
   * Performs a PayPal MassPay transaction via API using the passed MassPayRequest instance and the passed API Credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   */
  public static final MassPayResponseType massPay(MassPayRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    MassPayReq req = new MassPayReq(_request);
    MassPayResponseType response = getAPIInterface(_credentials).massPay(req);
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.massPay");
    return response;
  }
 
  /**
   * Performs a PayPal MassPay transaction via API using the passed MassPayRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   */
  public static final MassPayResponseType massPay(MassPayRequestType _request) throws RemoteException {
    return massPay(_request, DEFAULT_CREDENTIALS);
  }

  /**
   * Performs a PayPal Refund via API using the passed RefundTransactionRequest instance.
   * @param _request
   * @return
   * @throws SOAPException
   * @throws ServiceException
   * @throws RemoteException
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @see com.paypal.api.PayPalAPI.RefundTransactionResponseType
   * @see com.paypal.api.PayPalAPI.RefundTransactionRequestType
   */
  public static final RefundTransactionResponseType refundTransaction(RefundTransactionRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    RefundTransactionReq req = new RefundTransactionReq();
    req.setRefundTransactionRequest(_request);
    RefundTransactionResponseType response = getAPIInterface(_credentials).refundTransaction(req);
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.refundTransaction");
    return response;
  }

  /**
   * Performs a PayPal Refund via API using the passed RefundTransactionRequest instance.
   * @param _request
   * @return
   * @throws SOAPException
   * @throws ServiceException
   * @throws RemoteException
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @see com.paypal.api.PayPalAPI.RefundTransactionResponseType
   * @see com.paypal.api.PayPalAPI.RefundTransactionRequestType
   */
  public static final RefundTransactionResponseType refundTransaction(RefundTransactionRequestType _request) throws RemoteException {
    return refundTransaction(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal Refund via API using the passed data.
   * @param _txnId
   * @param _amount
   * @param _memo
   * @param _type
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final RefundTransactionResponseType refundTransaction(String _txnId, BasicAmountType _amount, String _memo, RefundPurposeTypeCodeType _type, APICredential _credentials) throws RemoteException {
    RefundTransactionRequestType request = new RefundTransactionRequestType();
    request.setVersion(CURRENT_VERSION);
    request.setTransactionID(_txnId);
    request.setAmount(_amount);
    request.setMemo(_memo);
    request.setRefundType(_type);
    return refundTransaction(request, _credentials);
  }
 
  /**
   * Performs a PayPal Refund via API using the passed data.
   * @param _txnId
   * @param _amount
   * @param _memo
   * @param _type
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final RefundTransactionResponseType refundTransaction(String _txnId, BasicAmountType _amount, String _memo, RefundPurposeTypeCodeType _type) throws RemoteException {
    return refundTransaction(_txnId, _amount, _memo, _type, DEFAULT_CREDENTIALS);
  }

  /**
   * Performs a PayPal Refund via API using the passed data.
   * @param _txnId
   * @param _amount
   * @param _currency
   * @param _memo
   * @param _type
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final RefundTransactionResponseType refundTransaction(String _txnId, String _amount, String _currency, String _memo, String _type, APICredential _credentials) throws RemoteException {
    RefundTransactionRequestType request = new RefundTransactionRequestType();
    request.setVersion(CURRENT_VERSION);
    BasicAmountType amount = new BasicAmountType(_amount);
    amount.setCurrencyID(CurrencyCodeType.fromString(_currency));
    request.setAmount(amount);
    request.setMemo(_memo);
    request.setRefundType(RefundPurposeTypeCodeType.fromString(_type));
    request.setTransactionID(_txnId);
   
    return refundTransaction(request, _credentials);
  }
 
  /**
   * Performs a PayPal Refund via API using the passed data.
   * @param _txnId
   * @param _amount
   * @param _currency
   * @param _memo
   * @param _type
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final RefundTransactionResponseType refundTransaction(String _txnId, String _amount, String _currency, String _memo, String _type) throws RemoteException {
    return refundTransaction(_txnId, _amount, _currency, _memo, _type, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal GetTransactionDetails API call using the passed
   * GetTransactionDetailsRequestType instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final GetTransactionDetailsResponseType getTransactionDetails(GetTransactionDetailsRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Initializing GetTransactionDetails API call, validating request for [" + _credentials.toString() + "]");
    }
    validateRequest(_request);
    GetTransactionDetailsReq request = new GetTransactionDetailsReq();
    request.setGetTransactionDetailsRequest(_request);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Executing GetTransactionDetails API call for [" + _credentials.toString() + "]");
    }
    GetTransactionDetailsResponseType response = getAPIInterface(_credentials).getTransactionDetails(request);
    if (LOG.isDebugEnabled()) {
      LOG.debug("GetTransactionDetails API call completed, processing response.");
    }
    processResponse(response);
    if (LOG.isDebugEnabled()) {
      LOG.debug("GetTransactionDetails response processed, returning.");
    }
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.getTransactionDetails");
    return response;
  }
 
  /**
   * Performs a PayPal GetTransactionDetails API call using the passed
   * GetTransactionDetailsRequestType instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final GetTransactionDetailsResponseType getTransactionDetails(GetTransactionDetailsRequestType _request) throws RemoteException {
    return getTransactionDetails(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal GetTransactionDetails API call using the passed Transaction ID.
   * @param _txnId
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final GetTransactionDetailsResponseType getTransactionDetails(String _txnId, APICredential _credentials) throws RemoteException {
    GetTransactionDetailsRequestType request = new GetTransactionDetailsRequestType();
    request.setVersion(CURRENT_VERSION);
    request.setTransactionID(_txnId);
    return getTransactionDetails(request, _credentials);
  }

  /**
   * Performs a PayPal GetTransactionDetails API call using the passed Transaction ID.
   * @param _txnId
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final GetTransactionDetailsResponseType getTransactionDetails(String _txnId) throws RemoteException {
    return getTransactionDetails(_txnId, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal TransactionSearch API call using the criteria specified in the passed
   * TransactionSearchRequestType instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final TransactionSearchResponseType transactionSearch(TransactionSearchRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Initializing TransactionSearch API call, validating request for [" + _credentials.toString() + "]");
    }
    validateRequest(_request);
    TransactionSearchReq request = new TransactionSearchReq();
    request.setTransactionSearchRequest(_request);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Executing TransactionSearch API call for [" + _credentials.toString() + "]");
    }
    TransactionSearchResponseType response = getAPIInterface(_credentials).transactionSearch(request);
    if (LOG.isDebugEnabled()) {
      LOG.debug("TransactionSearch API call completed, processing response.");
    }
    processResponse(response);
    if (LOG.isDebugEnabled()) {
      LOG.debug("TransactionSearch response processed, returning.");
    }
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.transactionSearch");
    return response;
  }
 
  /**
   * Performs a PayPal TransactionSearch API call using the criteria specified in the passed
   * TransactionSearchRequestType instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final TransactionSearchResponseType transactionSearch(TransactionSearchRequestType _request) throws RemoteException {
    return transactionSearch(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal TransactionSearch API call using the specified criteria.  You should
   * specify at least one parameter when performing your search.
   * @param _amount
   * @param _auctionItemNumber
   * @param _currencyCode
   * @param _endDate
   * @param _invoiceId
   * @param _emailAddress
   * @param _payerName
   * @param _receiptId
   * @param _receiver
   * @param _startDate
   * @param _status
   * @param _transactionClass
   * @param _txnId
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final TransactionSearchResponseType transactionSearch(BasicAmountType _amount, String _auctionItemNumber,
      CurrencyCodeType _currencyCode, Calendar _endDate, String _invoiceId,
      String _emailAddress, PersonNameType _payerName, String _receiptId,
      String _receiver, Calendar _startDate, PaymentTransactionStatusCodeType _status,
      PaymentTransactionClassCodeType _transactionClass,
      String _txnId, APICredential _credentials) throws RemoteException {
    TransactionSearchRequestType request = new TransactionSearchRequestType();
    request.setVersion(CURRENT_VERSION);
    if (_amount != null) {
      request.setAmount(_amount);
    }
    if (_auctionItemNumber != null) {
      request.setAuctionItemNumber(_auctionItemNumber);
    }
    if (_currencyCode != null) {
      request.setCurrencyCode(_currencyCode);
    }
    if (_endDate != null) {
      request.setEndDate(_endDate);
    }
    if (_invoiceId != null) {
      request.setInvoiceID(_invoiceId);
    }
    if (_emailAddress != null) {
      request.setPayer(_emailAddress);
    }
    if (_payerName != null) {
      request.setPayerName(_payerName);
    }
    if (_receiptId != null) {
      request.setReceiptID(_receiptId);
    }
    if (_receiver != null) {
      request.setReceiver(_receiver);
    }
    if (_startDate != null) {
      request.setStartDate(_startDate);
    }
    if (_status != null) {
      request.setStatus(_status);
    }
    if (_transactionClass != null) {
      request.setTransactionClass(_transactionClass);
    }
    if (_txnId != null) {
      request.setTransactionID(_txnId);
    }
   
    return transactionSearch(request, _credentials);
  }
 
  /**
   * Performs a PayPal TransactionSearch API call using the specified criteria.  You should
   * specify at least one parameter when performing your search.
   * @param _amount
   * @param _auctionItemNumber
   * @param _currencyCode
   * @param _endDate
   * @param _invoiceId
   * @param _emailAddress
   * @param _payerName
   * @param _receiptId
   * @param _receiver
   * @param _startDate
   * @param _status
   * @param _transactionClass
   * @param _txnId
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final TransactionSearchResponseType transactionSearch(BasicAmountType _amount, String _auctionItemNumber,
      CurrencyCodeType _currencyCode, Calendar _endDate, String _invoiceId,
      String _emailAddress, PersonNameType _payerName, String _receiptId,
      String _receiver, Calendar _startDate, PaymentTransactionStatusCodeType _status,
      PaymentTransactionClassCodeType _transactionClass,
      String _txnId) throws RemoteException {
    return transactionSearch(_amount, _auctionItemNumber, _currencyCode, _endDate, _invoiceId, _emailAddress, _payerName, _receiptId, _receiver, _startDate, _status, _transactionClass, _txnId, DEFAULT_CREDENTIALS);
  }
 
  /**
   *
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final SetExpressCheckoutResponseType setExpressCheckout(SetExpressCheckoutRequestDetailsType _request, APICredential _credentials) throws RemoteException {
    SetExpressCheckoutRequestType request = new SetExpressCheckoutRequestType();
    request.setVersion(CURRENT_VERSION);
    request.setSetExpressCheckoutRequestDetails(_request);
    return setExpressCheckout(request, _credentials);
  }
 
  /**
   * Performs a PayPal SetExpressCheckout API Call based on the passed SetExpressCheckoutRequest instance, using the default API credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final SetExpressCheckoutResponseType setExpressCheckout(SetExpressCheckoutRequestDetailsType _request) throws RemoteException {
    return setExpressCheckout(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal SetExpressCheckout API Call based on the passed SetExpressCheckoutRequest instance and the specified API credentials.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final SetExpressCheckoutResponseType setExpressCheckout(SetExpressCheckoutRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    SetExpressCheckoutResponseType response = getAPIAAInterface(_credentials).setExpressCheckout(new SetExpressCheckoutReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.setExpressCheckout");
    return response;
  }
 
  /**
   * Performs a PayPal SetExpressCheckout API Call based on the passed SetExpressCheckoutRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final SetExpressCheckoutResponseType setExpressCheckout(SetExpressCheckoutRequestType _request) throws RemoteException {
    return setExpressCheckout(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal GetExpressCheckoutDetails API Call based on the passed GetExpressCheckoutDetailsRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final GetExpressCheckoutDetailsResponseType getExpressCheckoutDetails(GetExpressCheckoutDetailsRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    GetExpressCheckoutDetailsResponseType response = getAPIAAInterface(_credentials).getExpressCheckoutDetails(new GetExpressCheckoutDetailsReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.getExpressCheckoutDetails");
    return response;
  }
 
  /**
   * Performs a PayPal GetExpressCheckoutDetails API Call based on the passed GetExpressCheckoutDetailsRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final GetExpressCheckoutDetailsResponseType getExpressCheckoutDetails(GetExpressCheckoutDetailsRequestType _request) throws RemoteException {
    return getExpressCheckoutDetails(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal GetExpressCheckoutDetails API Call based on the passed Token value.
   * @param _token
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final GetExpressCheckoutDetailsResponseType getExpressCheckoutDetails(String _token, APICredential _credentials) throws RemoteException {
    GetExpressCheckoutDetailsRequestType request = new GetExpressCheckoutDetailsRequestType();
    request.setVersion(CURRENT_VERSION);
    request.setToken(_token);
    return getExpressCheckoutDetails(request, _credentials);
  }
 
  /**
   * Performs a PayPal GetExpressCheckoutDetails API Call based on the passed Token value.
   * @param _token
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final GetExpressCheckoutDetailsResponseType getExpressCheckoutDetails(String _token) throws RemoteException {
    return getExpressCheckoutDetails(_token, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal DoExpressCheckoutPayment API Call based on the passed DoExpressCheckoutPaymentRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoExpressCheckoutPaymentResponseType doExpressCheckoutPayment(DoExpressCheckoutPaymentRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoExpressCheckoutPaymentResponseType response = getAPIAAInterface(_credentials).doExpressCheckoutPayment(new DoExpressCheckoutPaymentReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doExpressCheckoutPayment");
    return response;
  }
 
  /**
   * Performs a PayPal DoExpressCheckoutPayment API Call based on the passed DoExpressCheckoutPaymentRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoExpressCheckoutPaymentResponseType doExpressCheckoutPayment(DoExpressCheckoutPaymentRequestType _request) throws RemoteException {
    return doExpressCheckoutPayment(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   *
   * @param _requestDetails
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoExpressCheckoutPaymentResponseType doExpressCheckoutPayment(DoExpressCheckoutPaymentRequestDetailsType _requestDetails, APICredential _credentials) throws RemoteException {
    DoExpressCheckoutPaymentRequestType request = new DoExpressCheckoutPaymentRequestType(_requestDetails);
    request.setVersion(CURRENT_VERSION);
    return doExpressCheckoutPayment(request, _credentials);
  }
 
  /**
   *
   * @param _requestDetails
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoExpressCheckoutPaymentResponseType doExpressCheckoutPayment(DoExpressCheckoutPaymentRequestDetailsType _requestDetails) throws RemoteException {
    return doExpressCheckoutPayment(_requestDetails, DEFAULT_CREDENTIALS);
  }

  /**
   * Performs a PayPal DoReauthorization API call based on the passed DoReauthorizationRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoReauthorizationResponseType doReauthorization(DoReauthorizationRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoReauthorizationResponseType response = getAPIAAInterface(_credentials).doReauthorization(new DoReauthorizationReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doReauthorization");
    return response;
  }
 
  /**
   * Performs a PayPal DoReauthorization API call based on the passed DoReauthorizationRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoReauthorizationResponseType doReauthorization(DoReauthorizationRequestType _request) throws RemoteException {
    return doReauthorization(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal DoVoid API call based on the passed DoVoidRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoVoidResponseType doVoid(DoVoidRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoVoidResponseType response = getAPIAAInterface(_credentials).doVoid(new DoVoidReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doVoid");
    return response;
  }
 
  /**
   * Performs a PayPal DoVoid API call based on the passed DoVoidRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoVoidResponseType doVoid(DoVoidRequestType _request) throws RemoteException {
    return doVoid(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal DoCapture API call based on the passed DoCaptureRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoCaptureResponseType doCapture(DoCaptureRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoCaptureResponseType response = getAPIAAInterface(_credentials).doCapture(new DoCaptureReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doCapture");
    return response;
  }
 
  /**
   * Performs a PayPal DoCapture API call based on the passed DoCaptureRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoCaptureResponseType doCapture(DoCaptureRequestType _request) throws RemoteException {
    return doCapture(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Perform a PayPal DoDirectPayment API call based on the passed DoDirectPaymentRequest instance.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoDirectPaymentResponseType doDirectPayment(DoDirectPaymentRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoDirectPaymentResponseType response = getAPIAAInterface(_credentials).doDirectPayment(new DoDirectPaymentReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doDirectPayment");
    return response;
  }
 
  /**
   * Perform a PayPal DoDirectPayment API call based on the passed DoDirectPaymentRequest instance and the default Credentials.
   * @param _request
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   * @since 0.2.0
   */
  public static final DoDirectPaymentResponseType doDirectPayment(DoDirectPaymentRequestType _request) throws RemoteException {
    return doDirectPayment(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Perform a PayPal DoDirectPayment API call based on the passed DoDirectPaymentRequestDetails instance, using the specified API Credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoDirectPaymentResponseType doDirectPayment(DoDirectPaymentRequestDetailsType _request, APICredential _credentials) throws RemoteException {
    DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
    request.setDoDirectPaymentRequestDetails(_request);
    request.setVersion(CURRENT_VERSION);
    return doDirectPayment(request, _credentials);
  }
 
  /**
   * Perform a PayPal DoDirectPayment API call based on the passed DoDirectPaymentRequestDetails instance, using the default API credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   * @throws ServiceException
   * @throws SOAPException
   */
  public static final DoDirectPaymentResponseType doDirectPayment(DoDirectPaymentRequestDetailsType _request) throws RemoteException {
    return doDirectPayment(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal DoAuthorization API call based on the passed DoAuthorizationRequest instance, using the specified API Credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   */
  public static final DoAuthorizationResponseType doAuthorization(DoAuthorizationRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    DoAuthorizationResponseType response = getAPIAAInterface(_credentials).doAuthorization(new DoAuthorizationReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.doDirectPayment");
    return response;
  }
 
  /**
   * Performs a PayPal DoAuthorization API call based on the passed DoAuthorizationRequest instance, using the default API Credentials.
   * @param _request
   * @return
   * @throws RemoteException
   */
  public static final DoAuthorizationResponseType doAuthorization(DoAuthorizationRequestType _request) throws RemoteException {
    return doAuthorization(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal BillUser API called based on the passed BillUserRequest instance, using the specified API Credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   */
  public static final BillUserResponseType billUser(BillUserRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    BillUserResponseType response = getAPIInterface(_credentials).billUser(new BillUserReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.billUser");
    return response;
  }
 
  /**
   * Performs a PayPal BillUser API call based on the passed BillUserRequest, using the default API Credentials.
   * @param _request
   * @return
   * @throws RemoteException
   */
  public static final BillUserResponseType billUser(BillUserRequestType _request) throws RemoteException {
    return billUser(_request, DEFAULT_CREDENTIALS);
  }
 
  /**
   * Performs a PayPal BillAgreementUpdate API call based on the passed BAUpdateRequest instance, using the specified API Credentials.
   * @param _request
   * @param _credentials
   * @return
   * @throws RemoteException
   */
  public static final BAUpdateResponseType billAgreementUpdate(BAUpdateRequestType _request, APICredential _credentials) throws RemoteException {
    long startTime = System.currentTimeMillis();
    validateRequest(_request);
    BAUpdateResponseType response = getAPIInterface(_credentials).billAgreementUpdate(new BillAgreementUpdateReq(_request));
    processResponse(response);
    long endTime = System.currentTimeMillis();
    logMessage(startTime, endTime, response, "API.billAgreementUpdate");
    return response;
  }
 
  /**
   * Performs a PayPal BillAgreementUpdate API call based on the passed BAUpdateRequest instance, using the default API Credentials.
   * @param _request
   * @return
   * @throws RemoteException
   */
  public static final BAUpdateResponseType billAgreementUpdate(BAUpdateRequestType _request) throws RemoteException {
    return billAgreementUpdate(_request, DEFAULT_CREDENTIALS);
  }
 
  public static final Calendar apiStringToCalendar(String _timestamp) throws ParseException {
    Calendar timestamp = Calendar.getInstance();
    timestamp.clear();
    timestamp.setTime(NVPConstants.TIMESTAMP_FORMAT.parse(_timestamp));
    timestamp.setTimeZone(TimeZone.getTimeZone("Zulu"));
    return timestamp;
  }
 
  public static final String apiCalendarToString(Calendar _timestamp) {
    return NVPConstants.TIMESTAMP_FORMAT.format(_timestamp.getTime());
  }
 
  /**
   * @param _startTime
   * @param _endTime
   * @param _response
   */
  private static final void logMessage(long _startTime, long _endTime, AbstractResponseType _response, String _method) {
    if (_response.getAck().equals(AckCodeType.Success) || _response.getAck().equals(AckCodeType.SuccessWithWarning)) {
      LOGM.logSuccess(_method, _startTime, _endTime);
    }
    else {
      LOGM.logFailure(_method, _startTime, _endTime, _response.getCorrelationID());
    }
  }
 
  private static class BaseNVPAPIImpl {
    private APICredential credentials;
    protected HttpClient client;
   
    /**
     * @param credentials
     */
    public BaseNVPAPIImpl(APICredential _credentials) {
      credentials = _credentials;
      client = new HttpClient();
      client.getParams().setSoTimeout(getApiTimeout());
      client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(getApiRetry(), false));
      client.getParams().setContentCharset("UTF-8");
    }
   
    /**
     * @return the credentials
     */
    public final APICredential getCredentials() {
      return credentials;
    }

    /**
     * @param credentials the credentials to set
     */
    public final void setCredentials(APICredential credentials) {
      this.credentials = credentials;
    }
  }
 
  /**
   * Implementation class for the PayPalAPIInterface interface.
   * @author Michael Blanton (mike@mikeblanton.com)
   */
  private static final class NVPAPIImpl extends BaseNVPAPIImpl implements PayPalAPIInterface {

    public NVPAPIImpl(APICredential credentials) {
      super(credentials);
    }

    public AddressVerifyResponseType addressVerify(AddressVerifyReq addressVerifyRequest) throws RemoteException {
      throw new RemoteException("APIException while processing AddressVerify API call", new APIException("AddressVerify API call is not supported."));
    }

    public BAUpdateResponseType billAgreementUpdate(BillAgreementUpdateReq billAgreementUpdateRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(billAgreementUpdateRequest.getBAUpdateRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new BAUpdateResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }       
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public BillUserResponseType billUser(BillUserReq billUserRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(billUserRequest.getBillUserRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new BillUserResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public EnterBoardingResponseType enterBoarding(EnterBoardingReq enterBoardingRequest) throws RemoteException {
      throw new RemoteException("APIException while processing EnterBoarding API call", new APIException("EnterBoarding API call is not supported."));
    }

    public GetBoardingDetailsResponseType getBoardingDetails(GetBoardingDetailsReq getBoardingDetailsRequest) throws RemoteException {
      throw new RemoteException("APIException while processing GetBoardingDetails API call", new APIException("GetBoardingDetails API call is not supported."));
    }

    public GetTransactionDetailsResponseType getTransactionDetails(GetTransactionDetailsReq getTransactionDetailsRequest) throws RemoteException {
     
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(getTransactionDetailsRequest.getGetTransactionDetailsRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new GetTransactionDetailsResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public MassPayResponseType massPay(MassPayReq massPayRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(massPayRequest.getMassPayRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new MassPayResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public RefundTransactionResponseType refundTransaction(RefundTransactionReq refundTransactionRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(refundTransactionRequest.getRefundTransactionRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new RefundTransactionResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public TransactionSearchResponseType transactionSearch(TransactionSearchReq transactionSearchRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiUrl().toExternalForm());
      post.addParameters(transactionSearchRequest.getTransactionSearchRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new TransactionSearchResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }
   
  }
 
  /**
   * Implementation class for PaytPalAPIAA Interface
   * @author Michael Blanton (mike@mikeblanton.com)
   */
  private static final class NVPAAAPIImpl extends BaseNVPAPIImpl implements PayPalAPIAAInterface {

    public NVPAAAPIImpl(APICredential credentials) {
      super(credentials);
    }

    public DoAuthorizationResponseType doAuthorization(DoAuthorizationReq doAuthorizationRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doAuthorizationRequest.getDoAuthorizationRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoAuthorizationResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public DoCaptureResponseType doCapture(DoCaptureReq doCaptureRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doCaptureRequest.getDoCaptureRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoCaptureResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public DoDirectPaymentResponseType doDirectPayment(DoDirectPaymentReq doDirectPaymentRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doDirectPaymentRequest.getDoDirectPaymentRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoDirectPaymentResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (ParseException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public DoExpressCheckoutPaymentResponseType doExpressCheckoutPayment(DoExpressCheckoutPaymentReq doExpressCheckoutPaymentRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doExpressCheckoutPaymentRequest.getDoExpressCheckoutPaymentRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoExpressCheckoutPaymentResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (ParseException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public DoReauthorizationResponseType doReauthorization(DoReauthorizationReq doReauthorizationRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doReauthorizationRequest.getDoReauthorizationRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoReauthorizationResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public DoVoidResponseType doVoid(DoVoidReq doVoidRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(doVoidRequest.getDoVoidRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new DoVoidResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public GetExpressCheckoutDetailsResponseType getExpressCheckoutDetails(GetExpressCheckoutDetailsReq getExpressCheckoutDetailsRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      post.addParameters(getExpressCheckoutDetailsRequest.getGetExpressCheckoutDetailsRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new GetExpressCheckoutDetailsResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }

    public SetExpressCheckoutResponseType setExpressCheckout(SetExpressCheckoutReq setExpressCheckoutRequest) throws RemoteException {
      PostMethod post = new PostMethod(getApiAAUrl().toExternalForm());
      //post.addParameters(setExpressCheckoutRequest.getSetExpressCheckoutRequest().getNVPFields(getCredentials()));
      post.setRequestBody(setExpressCheckoutRequest.getSetExpressCheckoutRequest().getNVPFields(getCredentials()));
      try {
        client.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_OK) {
          return new SetExpressCheckoutResponseType(post.getResponseBodyAsStream());
        }
        else {
          throw new RemoteException("Error processing request [" + post.getStatusCode() + ": " + post.getStatusText() + "]");
        }
      } catch (HttpException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IOException e) {
        throw new RemoteException("Error processing request.", e);
      } catch (IllegalArgumentException e) {
        throw new RemoteException("Error processing request.", e);
      }
    }
   
  }
 
}
TOP

Related Classes of org.javacommerce.paypal.ws.API

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.